package servlets;


import bean.Bill;
import bean.Book;
import dao.BillDao;
import dao.Bookdao;
import dao.Cartdao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@WebServlet("/pay")
public class pay extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private ArrayList noten;
    public pay() {
        super();
        noten = new ArrayList();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        doPost(request, response);
    }
    private void delivergoods(Bill bill) {
        Bookdao bookdao = new Bookdao();
        ArrayList arrayList = bill.getList();
        for(Object it : arrayList) {
            Book book = (Book) it;
            bookdao.updateNum(book,  -book.getNum());
        }
    }
    // 判断逻辑，判断库存是否够
    private boolean ok(ArrayList list) {
        Bookdao bookdao = new Bookdao();
        boolean flag = true;
        for(Object it : list) {
            Book book = (Book)it;
            Book ku = bookdao.getBook(book.getIsbn());
            if(ku.getNum() < book.getNum()) {
                flag = false;
                noten.add(book);
            }
        }
        return  flag;

    }
    // 获取当前时间转换成字符串
    private String now() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        HttpSession ss =  request.getSession();
        String user_name = (String) ss.getAttribute("name");
        BillDao billDao = new BillDao();
        Cartdao cartdao = new Cartdao();
        ArrayList list = cartdao.getList(user_name);
        if(ok(list)) {
            System.out.println("01294301294012930219309302193012");
            // 删除购物车，跳转支付页面，并生成账单，对库存进行操作
            Bill bill = new Bill();
            bill.setMoney(cartdao.getSumPrice(user_name));  // 填购物车的
            bill.setUser_name(user_name);
            bill.setAddr(request.getParameter("addr"));
            bill.setDate(now());
            bill.setList(list);
            bill.setIsok(false);
            billDao.addBill(bill);
            cartdao.clear(user_name);
            delivergoods(bill);
            PrintWriter out = response.getWriter();
            out.println("<h1>" + "支付成功" + "</h1>");

            response.sendRedirect("main.jsp?id=0");
        }
        else {
            // 跳转库存不够页面，并返回到购物车页面
            PrintWriter out = response.getWriter();
            out.println("<h1>" + "以下图书库存不足" + "</h1>");
            for (Object it: noten) {
                Book book = (Book)it;
                out.println("<h1>" + "《" + book.getName() + "》" +  "</h1>");
            }

            response.sendRedirect("cartlist.jsp");
        }
    }
}
